import { Component, OnInit, Input } from '@angular/core'
import { IBaseInfoColumn } from '@shared/interfaces/baseInfo'
import { Task, ETaskStatusMap, ETaskCategory, ETaskTypeMap } from '@shared/interfaces/task'
import { DetailBaseClass } from '@shared/classes/DetailBaseClass'
import { I18NService } from '@core'
import { Router, ActivatedRoute } from '@angular/router'
import { TitleService } from '@delon/theme'
import { PageService } from '@shared/services/page.service'
import { pipe } from '@angular/core/src/render3'

@Component({
  selector: 'app-task-base-info',
  templateUrl: './task-base-info.component.html',
})
export class TaskBaseInfoComponent extends DetailBaseClass<Task> implements OnInit {

  @Input() taskInfo: Task
  firstColumns: IBaseInfoColumn[] = []
  secondColumns: IBaseInfoColumn[] = []

  constructor(
    public i18n: I18NService,
    public router: Router,
    public route: ActivatedRoute,
    public titleService: TitleService,
    public pageService: PageService,
  ) {
    super(pageService)
 }

  ngOnInit() {
    this.detailData = this.taskInfo
    this.initFirstColumns()
    this.initSecondColumns()
    this.initColumns()
  }

  initFirstColumns(): void {
    this.firstColumns = [
      {
        span: 6,
        index: 'jobID',
        label: '任务编号',
        labelI18n: 'TASK.AUDIT.JOBID'
      },
      {
        span: 6,
        index: 'auditType',
        label: '任务类别',
        labelI18n: 'TASK.AUDIT.AUDITTYPE',
        pipe: (auditType) => this.parseAuditType(auditType),
      },
      {
        span: 6,
        index: 'creator',
        label: '申请用户',
        labelI18n: 'TASK.AUDIT.CREATOR'
      },
      {
        span: 6,
        index: 'status',
        label: '任务状态',
        labelI18n: 'TASK.AUDIT.STATUS',
        pipe: (status) => this.parseAuditStatus(status),
      },
    ]
  }


  initSecondColumns(): void {
    this.secondColumns = [
      {
        span: 6,
        index: 'createTime',
        label: '创建时间',
        labelI18n: 'GENERAL.INFO.CREATED_AT',
        type: 'date',
      },
      {
        span: 6,
        index: 'updateTime',
        label: '更新时间',
        labelI18n: 'GENERAL.INFO.UPDATED_AT',
        type: 'date'
      },
      {
        span: 6,
        index: '',
        label: '审批链条',
        labelI18n: 'GENERAL.INFO.AUDIT_CHAIN',
        value: this.getAuditLink()
      },
      {
        span: 6,
        index: '',
        label: '当前指派用户',
        labelI18n: 'TASK.USER.CURRENT_ASSIGN',
        value: this.getCurrentOperators()
      },
    ]
  }

  parseAuditType(auditType: string): string {
    try {
      return this.trans(ETaskTypeMap[auditType])
    } catch (e) {
      return this.trans('GENERAL.INFO.NONE')
    }
  }

  parseAuditStatus(status: string): string {
    try {
      return this.trans(ETaskStatusMap[status])
    } catch (e) {
      return this.trans('GENERAL.INFO.NONE')
    }
  }

  private getAuditLink(): string {
    const preOperators = this.detailData.preOperators || []
    return preOperators.join('->')
  }

  private getCurrentOperators(): string {
    const currentOperators = this.detailData.currentOperators || []
    return currentOperators.length === 0 ? this.trans('GENERAL.INFO.NONE') : currentOperators.join(',')
  }

  private trans(key: string, params?: Object): string {
    return this.i18n.fanyi(key, params)
  }
}
